/*
 * GraphL.cpp
 *
 *  Created on: 2012-04-03
 *      Author: tony
 */

#include "GraphL.h"

// When using templates, we either put all the code into the .h file
// or we include the .cpp file, if we have one
#include "Queue.h"
#include "Queue.cpp"

namespace graph {

GraphL::GraphL() {
	std::cout << "  GraphL constructor - no args" << std::endl;
	list = NULL;
}

GraphL::GraphL(const int n) {
	std::cout << "  GraphL constructor - arg = " << n << std::endl;
	list = new AdjacencyList( n );
}

GraphL::~GraphL() {
	std::cout << "  GraphL destructor" << std::endl;
	if (list != NULL)
		delete list;
}

void GraphL::addEdge(const int i, const int j, const float w){
	std::cout << "   GraphL addEdge - " << i << " " << j << " " << w << std::endl;
	if ( list == NULL )
		list = new AdjacencyList();

	list->addEdge( i, j, w );
}

void GraphL::removeEdge(const int i, const int j){
	if ( list == NULL )
		return;
	list->removeEdge( i, j );
}

BinaryTree* GraphL::createTree(){
	/*
	 * This method will set up a queue of vertices
	 * and construct a binary tree of vertex information.
	 */
	Queue<Vertex*> vertexQueue;
	// get all the vertices from the Adjacency list:
	std::vector<Vertex*>* v = list->getListOfVertices();
	std::vector<Vertex*>::iterator iter;
	/*
	 *  We could use a vector as a queue but then we
	 *  would not have this cool application of Queues.
	 */
	for ( iter=v->begin(); iter != v->end(); ++iter )
		vertexQueue.enqueue( new Vertex( *(*iter) ) );

	// Construct a binary tree based on the information in the
	// graph - we will use the queue to help us order the nodes.
	BinaryTree* tree = NULL;

	// This is a test
	int junk = 0;
	while (! vertexQueue.is_empty() ){
		if (tree == NULL){
			// set the root node
			Vertex* tempVertex = vertexQueue.dequeue();
			tree = new BinaryTree( new TreeNode( *tempVertex ) );
		} else {
			Vertex* tempVertex = vertexQueue.dequeue();

			BinaryTree* temp = new BinaryTree( new TreeNode( *tempVertex ) );

			if ( (junk++ % 2) == 0 )
				tree->attachLeft( temp );
			else
				tree->attachRight( temp );

			delete temp;
		}
	}

	return tree;
}

} /* namespace graph */
